home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14078 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  37 lines

  1. Newsgroups: comp.lang.c,comp.unix.programmer
  2. Path: howland.reston.ans.net!torn!sq!msb
  3. From: msb@sq.com (Mark Brader)
  4. Subject: Re: Q: '\n' character
  5. Message-ID: <1996Apr11.192937.25676@sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <4kj66f$k0o@ren.cei.net>
  8. Date: Thu, 11 Apr 1996 19:29:37 GMT
  9.  
  10. > > Is there a function or some sort of way that I could remove '\n'
  11. > > charecter form the end of the string. 
  12. > fgets(buffer, file); 
  13. > buffer[strlen(buffer)-1]=0;  
  14. > It may not be the fastest, but it is darned near the easiest. 
  15.  
  16. Or the buggiest.  Instead use:
  17.  
  18.   len = strlen (buffer);
  19.   if (len > 0 && buffer[len-1] == '\n') buffer[len-1] = '\0';
  20.  
  21. Both tests in the "if" are necessary.
  22.  
  23. In the specific case of a string obtained from fgets(), we can be
  24. sure that if there is a newline then it is the last character.
  25. This leads to the alternative approach:
  26.  
  27.   ptr = strchr (buffer, '\n');   /* or strrchr() */
  28.   if (ptr) *ptr = '\0';
  29. -- 
  30. Mark Brader, msb@sq.com, SoftQuad Inc., Toronto
  31.         "I'm a little worried about the bug-eater," she said.  "We're embedded
  32.         in bugs, have you noticed?"             -- Niven, "The Integral Trees"
  33.  
  34. My text in this article is in the public domain.
  35.